package com.yfux._05_tlias.service.impl;

import com.yfux._05_tlias.mapper.DeptMapper;
import com.yfux._05_tlias.pojo.Dept;
import com.yfux._05_tlias.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        log.info("查询所有员工数据.");
        return deptMapper.list();
    }

    @Override
    public Integer deleteById(Integer id) {
        log.info("删除Id为{}的数据", id);
        return deptMapper.deleteById(id);
    }

    @Override
    public Integer insert(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        log.info("添加数据: {}", dept);
        return deptMapper.insert(dept);
    }

    @Override
    public Dept findById(Integer id) {
        log.info("查询Id为{}的部门", id);
        return deptMapper.findById(id);
    }

    @Override
    public Integer update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        log.info("修改数据, 修改后: {}", dept);
        if (dept.getName().isBlank()) {
            log.error("dept的名称不能为空白.");
            return 0;
        }
        return deptMapper.update(dept);
    }
}
